<?php
Zend_Loader::loadClass("Application_Ajax_Server_Function");
Zend_Loader::loadClass('Gases');
class GetGasDetailsById extends Application_Ajax_Server_Function
{
	public function call() {
		$id = (int) $this->_params['id'];
		$gases = new Gases();
		$recordSet = $gases->find($id);
		$gas = $recordSet->current();
		
		//Zend_Debug::dump($gas->id_category);exit;
		// Build prev/next circular array
		if (!$gas->id_category) {
			$recordSet = $gases->select()->from($gases, array("id"))
			->where("id_category is null")
			->where("status > ?", 0)
			->order("name");
		} else {
			$recordSet = $gases->select()->from($gases, array("id"))
			->where("id_category = ?", (int)$gas->id_category)
			->where("status > ?", 0)
			->order("name");
		}
		
		//Zend_Debug::dump($recordSet);
		$ids = $gases->fetchAll($recordSet)->toArray();
		foreach ($ids as $k => $v)
		{
			if($v["id"] == $id)
			{
				if (isset($ids[$k-1]))
					$prev = $ids[$k-1];
				else
					$prev = end($ids);
				if (isset($ids[$k+1]))
					$next = $ids[$k+1];
				else
					$next = $ids[0];
			}
		}

		$result = array(
				$gas->toArray(),
				'category' => $gas->findParentRow('GasCategories') ? $gas->findParentRow('GasCategories')->name : null,
				'prev' => $prev["id"],
				'next' => $next["id"]

		);

		return $result;
	}
}
